package com.ruoyi.jihuo.service.handler;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.jihuo.domain.JhTmGoods;
import com.ruoyi.jihuo.domain.Sku;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：LXY
 * @DATE：2025-09-14 15:16
 * @Description：SKU销售属性处理器
 */
@Slf4j
@Component
public class Jh77PriceHandler implements PriorityHandler {
    @Override
    public int getPriority() {
        return 77;
    }

    @Override
    public boolean handle(ProcessContext<JSONObject> request) throws IOException {
        JSONObject data = request.getData();
        JhTmGoods item = data.getObject("item", JhTmGoods.class);
        JSONArray jhSkus = item.getJhSkus();
        JSONArray jhSaleProps = item.getJhSaleProps();
        List<Sku> skuList = new ArrayList<>();
        HashMap<String, String> salePropMap = new HashMap<>();//vId:vName
        if ((jhSkus ==null || jhSkus.isEmpty()) && (jhSaleProps ==null || jhSaleProps.isEmpty())){
            request.setErrorInfo("商品SKU或销售属性为空");
            throw new RuntimeException("商品SKU或销售属性为空");
        }
        try {
            for (Object jsonNode : jhSaleProps) {
                JSONObject salePropJson = (JSONObject) jsonNode;
                JSONArray values = salePropJson.getJSONArray("values");
                for (Object value : values) {
                    JSONObject valueJson = (JSONObject) value;
                    salePropMap.put(valueJson.getString("vId"), valueJson.getString("vName"));
                }
            }
            for (Object jsonNode : jhSkus) {
                JSONObject skuJson = (JSONObject) jsonNode;
                Sku sku = new Sku();
                sku.setSkuOrigPrice(skuJson.getString("skuOrigPrice"));
                sku.setSkuDisPrice(skuJson.getString("skuDisPrice"));
                sku.setSkuPromoPrice(skuJson.getString("skuPromoPrice"));
                sku.setSkuQuantity(skuJson.getString("skuQuantity"));
                JSONArray propsJson = ((JSONObject) jsonNode).getJSONArray("props");
                List<Sku.prop> props = new ArrayList<>();
                for (Object propNode : propsJson) {
                    JSONObject propJson = (JSONObject) propNode;
                    Sku.prop prop = new Sku.prop();
                    prop.setPId(propJson.getString("pId"));
                    prop.setVId(propJson.getString("vId"));
                    prop.setVName(salePropMap.getOrDefault(propJson.getString("vId"), "-"));
                    props.add(prop);
                }
                sku.setProps(props);
                skuList.add(sku);

            }
            data.put("skuList", skuList);
        }catch (Exception e){
            request.setErrorInfo("SKU销售属性处理异常:"+e.getMessage());
            throw new RuntimeException("SKU销售属性处理异常:"+e.getMessage());
        }
        return true;
    }
}
